/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import databaseManager.DbConn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author 47
 */
public class Register {

    public Register() {
    }
    private String userName = null;
    private String userPassword = null;
    private String message = "";
    private HttpServletRequest httpRequest;

    public boolean getRegisterResult() {
        boolean isOK = false;
        if (this.userName != null && this.userPassword != null) {
            if (!this.userName.equals("") && !this.userPassword.equals("")) {
                databaseManager.DbConn dbconn = new DbConn();
                Connection conn = null;
                Statement stmt = null;
                String sql = "";
                try {
                    conn = dbconn.getNewDbconn();
                    stmt = conn.createStatement();
                    sql = "insert into usertable(user_name,user_password) values('" + this.getUserName() + "','" + this.getUserPassword() + "')";
                    stmt.execute(sql);
                    this.getHttpRequest().getSession().setAttribute("username", this.getUserName());
                    isOK = true;

                } catch (Exception ex) {
                    isOK = false;
                    this.setMessage(ex.getMessage());
                } finally {
                    if (stmt != null) {
                        try {
                            stmt.close();
                        } catch (Exception stmte) {
                        }
                    }
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (Exception conne) {
                        }
                    }
                }
            }
        }
        return isOK;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        try{
        if(userName!=null){
            this.userName=new String(userName.getBytes("ISO-8859-1"),"UTF-8");
        }
        }catch(Exception ex){
            this.message=ex.getMessage();
            this.userName=null;
        }
    }

    /**
     * @return the userPassword
     */
    public String getUserPassword() {
        return userPassword;
    }

    /**
     * @param userPassword the userPassword to set
     */
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the httpRequest
     */
    public HttpServletRequest getHttpRequest() {
        return httpRequest;
    }

    /**
     * @param httpRequest the httpRequest to set
     */
    public void setHttpRequest(HttpServletRequest httpRequest) {
        this.httpRequest = httpRequest;
    }
}
